package com.base.cn.platform.os.controller.course.open;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.open.CourseOpen;
import com.base.cn.platform.os.entity.course.open.CourseOpenBespeak;
import com.base.cn.platform.os.service.course.open.CourseOpenBespeakBiz;
import com.base.cn.platform.os.service.course.open.CourseOpenBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 免费直播预约Controller
 *
 * @author s.li
 * @create 2018-09-11-16:19
 */
@RestController
@RequestMapping("/course")
public class CourseOpenBespeakController extends BaseController {

    @Autowired
    private CourseOpenBespeakBiz courseOpenBespeakBiz;
    @Autowired
    private CourseOpenBiz courseOpenBiz;

    /**
     * 预约免费直播
     * @param courseOpenBespeak
     * @return Map<String,Object>
     */
    @RequestMapping("/saveBespeak")
    public Map<String,Object> saveBespeak(@RequestBody CourseOpenBespeak courseOpenBespeak){
        if(!DataUtil.idIsNotNull(courseOpenBespeak.getUserId())){
            return ResultUtil.ERROR("请登录后再预约");
        }
        CourseOpen courseOpen = courseOpenBiz.findCourseOpenById(courseOpenBespeak.getOpenCourseId(),false,false,false);
        if(ObjectUtils.isEmpty(courseOpen)){
            return ResultUtil.ERROR("免费直播不存在");
        }
        //1正在直播 ，2未开始，3已结束
        if(courseOpen.getPlayStatus()!=2){
            return ResultUtil.ERROR("免费直播不可预约");
        }
        CourseOpenBespeak _courseOpenBespeak = courseOpenBespeakBiz.getUserCourseOpenBespeak(courseOpenBespeak.getUserId(),courseOpenBespeak.getOpenCourseId());
        if(ObjectUtils.isNotEmpty(_courseOpenBespeak)){
            return ResultUtil.ERROR("您已预约过该免费直播");
        }
        courseOpenBespeakBiz.saveBespeak(courseOpenBespeak,courseOpen);
        return ResultUtil.SUCCESS("免费直播预约成功");
    }
}
